using System;
using System.ComponentModel.Composition;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ICP_MS.Plugin.Experiment;
using ICP_MS.Plugin.iMass.Properties;

namespace ICP_MS.Plugin.iMass;

internal class iMassM
{
	private AppManager app;

	private ToolStripZedgraph toolZedgraph;

	private LabelActionItem vacuumStatus;

	private LabelActionItem turboValues;

	private LabelActionItem vacuumValues;

	private LabelActionItem plasmaWarns;

	private LabelActionItem platWarns;

	private ImageStatusPanel connPanel;

	private ImageStatusPanel plasmaPanel;

	private ImageStatusPanel logPannel;

	private LabelStatusPanel userlabel;

	private LabelStatusPanel username;

	private Image plasmaOn;

	private Image plasmaOff;

	private Image connOK;

	private Image connError;

	private Image connWarn;

	private Image LogInfo;

	private bool CancelPlasma = false;

	private int IgniteStep = 0;

	private const string toolBar = "kValueBar";

	private int tickCount = 0;

	private int VacuumErrorOldState = 0;

	private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

	private AutoResetEvent[] valveEvents = new AutoResetEvent[2]
	{
		new AutoResetEvent(initialState: false),
		new AutoResetEvent(initialState: false)
	};

	private Thread taskOnPlasma = null;

	private Thread taskOffPlasma = null;

	private bool IsInXYZAxis = false;

	private bool _ifErr = false;

	[Import("Shell", typeof(ContainerControl))]
	private ContainerControl Shell { get; set; }

	public iMassM(AppManager app)
	{
		if (app == null)
		{
			throw new Exception("Null AppManager!");
		}
		this.app = app;
		Shell = app.Shell;
	}

	public void Init()
	{
		plasmaOff = Resources.PlasmaOff;
		plasmaOn = Resources.PlasmaOn;
		connOK = Resources.connect;
		connError = Resources.disconnect;
		connWarn = Resources.disconnectX;
		LogInfo = Resources.LogInfo;
		IHeaderControl header = app.HeaderControl;
		IStatusControl status = app.StatusControl;
		header.Add(new RootItem("kWindow", Lang.Txt("iMass.Menu.Window.Text"))
		{
			SortOrder = 300
		});
		header.Add(new SimpleActionItem("kWindow", Lang.Txt("iMass.Menu.Window.Cascading.Text"), Layout_Cascade));
		header.Add(new SimpleActionItem("kWindow", Lang.Txt("iMass.Menu.Window.Tile.Text"), Layout_Tile));
		header.Add(new SimpleActionItem("kWindow", Lang.Txt("iMass.Menu.Window.Arrange.Text"), Layout_Arrange));
		header.Add(new SeparatorItem("kWindow", "kWindow"));
		header.Add(new SimpleActionItem("kWindow", Lang.Txt("iMass.Menu.Close.Current.Text"), Forms_CloseCurrent));
		header.Add(new SimpleActionItem("kWindow", Lang.Txt("iMass.Menu.Close.All.Text"), Forms_CloseCurrent));
		header.Add(new SeparatorItem("kWindow", "kWindow"));
		header.Add(new RootItem("kUser", Lang.Txt("iMass.Menu.User.Text"))
		{
			SortOrder = 300
		});
		header.Add(new SimpleActionItem("kUser", Lang.Txt("iMass.Menu.User.SwitchUser.Text"), ChangeUser));
		header.Add(new SimpleActionItem("kUser", Lang.Txt("iMass.Menu.User.Management.Text"), ManageUsers));
		header.Add(new SimpleActionItem("kTool", Lang.Txt("iMass.Menu.Tool.EmergencyExtinguishing.Text"), PlasmaOffForEmergency));
		header.Add(new SimpleActionItem("kTool", Lang.Txt("iMass.Menu.Tool.MonitorManage.Text"), MonitorManage));
		vacuumStatus = new LabelActionItem(Lang.Txt("iMass.VaccumOn.State.Stay.Text"))
		{
			GroupCaption = "kValueBar"
		};
		header.Add(vacuumStatus);
		header.Add(new SeparatorItem
		{
			GroupCaption = "kValueBar"
		});
		turboValues = new LabelActionItem("0000 Hz,0000 Hz")
		{
			GroupCaption = "kValueBar",
			Width = 110
		};
		header.Add(turboValues);
		header.Add(new SeparatorItem
		{
			GroupCaption = "kValueBar"
		});
		vacuumValues = new LabelActionItem(Lang.Txt("iMass.Vaccum.Int.Value.Text") + "0.00e00 Pa;" + Lang.Txt("iMass.Vaccum.Lens.Value.Text") + "0.00e00 Pa; " + Lang.Txt("iMass.Vaccum.Quad.Value.Text") + "0.00e00 Pa")
		{
			GroupCaption = "kValueBar",
			Width = 500
		};
		header.Add(vacuumValues);
		header.Add(new SeparatorItem
		{
			GroupCaption = "kValueBar"
		});
		plasmaWarns = new LabelActionItem("")
		{
			GroupCaption = "kValueBar"
		};
		header.Add(plasmaWarns);
		header.Add(new SeparatorItem
		{
			GroupCaption = "kValueBar"
		});
		platWarns = new LabelActionItem("")
		{
			GroupCaption = "kValueBar"
		};
		header.Add(platWarns);
		header.Add(new SimpleActionItem(Lang.Txt("iMass.ToolBar.VacuumOn.Text"), VacuumOn_Click)
		{
			Direction = Direction.Horizontal,
			GroupCaption = "Control",
			LargeImage = Resources.VacuumOn,
			SmallImage = Resources.VacuumOn,
			Height = 55,
			Width = 90,
			Size = 90
		});
		header.Add(new SimpleActionItem(Lang.Txt("iMass.ToolBar.VaccumOff.Text"), VacuumOff_Click)
		{
			Direction = Direction.Horizontal,
			GroupCaption = "Control",
			LargeImage = Resources.VacuumOff,
			SmallImage = Resources.VacuumOff,
			Height = 55,
			Width = 90,
			Size = 90
		});
		header.Add(new SeparatorItem
		{
			GroupCaption = "Control"
		});
		header.Add(new SimpleActionItem(Lang.Txt("iMass.ToolBar.PlasmaOn.Text"), PlasmaOn_Click)
		{
			Direction = Direction.Horizontal,
			GroupCaption = "Control",
			LargeImage = Resources.PlasmaOn,
			SmallImage = Resources.PlasmaOn,
			Height = 55,
			Width = 80,
			Size = 80
		});
		header.Add(new SimpleActionItem(Lang.Txt("iMass.ToolBar.PlasmaOff.Text"), PlasmaOff_Click)
		{
			Direction = Direction.Horizontal,
			GroupCaption = "Control",
			LargeImage = Resources.PlasmaOff,
			SmallImage = Resources.PlasmaOff,
			Height = 55,
			Width = 80,
			Size = 80
		});
		header.Add(new SeparatorItem
		{
			GroupCaption = "Control"
		});
		toolZedgraph = new ToolStripZedgraph();
		toolZedgraph.Alignment = ToolStripItemAlignment.Right;
		header.Add(new CustomActionItem(toolZedgraph)
		{
			GroupCaption = "Control"
		});
		header.Add(new SimpleActionItem(Lang.Txt("iMass.Experiment.EmergencyExtinguishing.Text"), PlasmaOffForEmergency)
		{
			Direction = Direction.Vertical,
			GroupCaption = "Advance",
			LargeImage = Resources.PlasmaOff1,
			SmallImage = Resources.PlasmaOff1,
			ToggleGroupKey = "kLeftBar",
			Height = 55,
			Width = 80,
			Size = 80,
			Allignment = 1
		});
		plasmaPanel = new ImageStatusPanel(plasmaOff, null)
		{
			ToolTipText = Lang.Txt("iMass.StatusBar.Plasma.Off.Text")
		};
		status.Add(plasmaPanel);
		connPanel = new ImageStatusPanel(connError, null)
		{
			ToolTipText = Lang.Txt("iMass.StatusBar.Connect.Error.Text")
		};
		status.Add(connPanel);
		CAN.logMsg = (LogMsg)Delegate.Combine(CAN.logMsg, new LogMsg(CanetMsg));
		Detector.Instance.errMsg += Detector_errMsg;
		PlasmaManager.errMsg += Detector_errMsg;
		Detector.analogReceive += Detector_analogReceive;
		PlasmaManager.AbnormalChecked += PlasmaStatus_AbnormalChecked;
		Vacuum.errorEvent += Vacuum_errorEvent;
		PlatManager.errorEvent += PlatManager_errorEvent;
		app.Tick += Tick_InstrumentStatus;
		UPSManager.Instance.Init();
		UPSManager.errorEvent += UPSManager_errorEvent;
		iMassProcess.CMDEvent += IMassProcess_CMDEvent;
		iMassProcess.Instance.Init();
		timer.Interval = 2000;
		timer.Tick += timer_Tick;
		timer.Start();
		ExperimentDatas.autoMisfire += Experiment_autoMisfire;
	}

	private void Experiment_autoMisfire(object sender, EventArgs e)
	{
		taskOffPlasma = new Thread(OffPlasma);
		taskOffPlasma.Start(true);
	}

	private void IMassProcess_CMDEvent(object sender, CMDEventArgs e)
	{
		switch (e.CMD)
		{
		case 1:
			if ((taskOffPlasma == null || !taskOffPlasma.IsAlive) && Vacuum.vacumm.ReadMode >= 2 && PlasmaManager.plasma.StatusPlasma != 7)
			{
				taskOnPlasma = new Thread(OnPlasma);
				taskOnPlasma.Start(true);
			}
			break;
		case 2:
			if (taskOffPlasma == null || !taskOffPlasma.IsAlive)
			{
				taskOffPlasma = new Thread(OffPlasma);
				taskOffPlasma.Start(true);
			}
			break;
		}
	}

	private void timer_Tick(object sender, EventArgs e)
	{
		Log.InsertInstruStatus(PlasmaManager.plasma.StatusPlasma, PlasmaManager.plasma.ReadInterfaceTemperature, Vacuum.vacumm.ReadVacuum[0], Vacuum.vacumm.ReadVacuum[1], Vacuum.vacumm.ReadVacuum[2], PlasmaManager.plasma.ReadTemperatureWind, PlasmaManager.plasma.ReadWater, PlatManager.plat.ReadX / 100.0, PlatManager.plat.ReadY / 100.0, PlatManager.plat.ReadZ / 100.0, PlasmaManager.plasma.ReadNebuliser, PlasmaManager.plasma.ReadReflection, PlasmaManager.plasma.ReadForward, PlasmaManager.plasma.ReadCool, PlasmaManager.plasma.ReadAuxiliary, (double)PlatManager.injector.ReadPerist * 0.1);
	}

	private void UPSManager_errorEvent(object sender, EventArgs e)
	{
		if (PlasmaManager.plasma.StatusPlasma == 7)
		{
			if (taskOffPlasma == null || !taskOffPlasma.IsAlive)
			{
				app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOff.Prepare.Text"));
				LensManager.Instance.CloseEtp();
				LensManager.Instance.CloseLens();
				Thread.Sleep(5000);
				if (Vacuum.vacumm.ReadMode > 2)
				{
					Vacuum.Instance.NewVacuumOff();
				}
				app.StatusControl.Progress(null, 10, Lang.Txt("iMass.PlasmaOff.VaccumOff.Text"));
				Thread.Sleep(20000);
				Plasma.Instance.PlasmaOff();
				PlasmaManager.plasma.StatusOldPlasma = 0;
				Thread.Sleep(1000);
				app.StatusControl.Progress(null, 30, Lang.Txt("iMass.PlasmaOff.breeze.Text"));
				PlasmaManager.plasma.Cool = 10.0;
				Plasma.Instance.OpenCoolGas();
				PlasmaManager.plasma.Auxiliary = 0.8;
				Plasma.Instance.OpenAuxiliaryGas();
				for (int i = 0; i < 30; i++)
				{
					app.StatusControl.Progress(null, 40 + i, string.Format(Lang.Txt("iMass.PlasmaOff.Cool.Text") + "{0}s..", (30 - i) * 2));
					Thread.Sleep(2000);
				}
				Plasma.Instance.CloseCoolGas();
				Plasma.Instance.CloseAuxiliaryGas();
				Plasma.Instance.CloseCollision();
				Plasma.Instance.CloseCollision2();
				app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOff.Finish.Text"));
				CancelPlasma = false;
			}
			int count = 0;
			while (PlasmaManager.plasma.StatusPlasma != 0)
			{
				Thread.Sleep(5000);
				if (count++ > 15)
				{
					break;
				}
			}
		}
		if (Vacuum.vacumm.ReadMode == 5)
		{
			Vacuum.Instance.NewVacuumOff();
		}
		Thread.Sleep(20000);
		if (Vacuum.vacumm.ReadMode == 2)
		{
			OffVacuum();
		}
	}

	private void MonitorManage(object sender, EventArgs e)
	{
		FormExperimentTest formExperimentTest = new FormExperimentTest();
		formExperimentTest.ShowDialog();
	}

	private void PlasmaOffForEmergency(object sender, EventArgs e)
	{
		PowerManager.Instance.CANET = false;
		RFManager.rfs.HVP = 0f;
		RFManager.rfs.HVN = 0f;
		RFManager.rfs.Gate = 0f;
		if (Detector.detectorConfig.IsNewDetector)
		{
			Detector.Instance.msDetectorRF.SetHVNNew();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetHVPNew();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetGateNew();
			Thread.Sleep(1);
		}
		else
		{
			Detector.Instance.msDetectorRF.SetHVN();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetHVP();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetGate();
			Thread.Sleep(1);
		}
		PowerManager.Instance.CANET = false;
		if (Vacuum.vacumm.ReadMode >= 4)
		{
			Thread.Sleep(10);
			Vacuum.Instance.NewVacuumOff();
		}
		Thread.Sleep(10);
		int count = 0;
		while (count < 10)
		{
			app.StatusControl.Progress(null, 10, Lang.Txt("iMass.PlasmaOff.VaccumOff.Text"));
			Thread.Sleep(1000);
			count++;
			if (Vacuum.vacumm.ReadMode == 2)
			{
				break;
			}
		}
		Plasma.Instance.PlasmaOff();
		PlasmaManager.plasma.StatusOldPlasma = 0;
		Log.InsertLog(Lang.Txt("iMass.Menu.Tool.EmergencyExtinguishing.Text"), Lang.Txt("AddiMass.Text32"), "", Severity.Info);
	}

	public void Dispose()
	{
		timer.Stop();
		app.Tick -= Tick_InstrumentStatus;
		CAN.logMsg = (LogMsg)Delegate.Remove(CAN.logMsg, new LogMsg(CanetMsg));
		Detector.Instance.errMsg -= Detector_errMsg;
		PlasmaManager.errMsg -= Detector_errMsg;
		Detector.analogReceive -= Detector_analogReceive;
		PlasmaManager.AbnormalChecked -= PlasmaStatus_AbnormalChecked;
		Vacuum.errorEvent -= Vacuum_errorEvent;
		PlatManager.errorEvent -= PlatManager_errorEvent;
		iMassProcess.Instance.Dispose();
		iMassProcess.CMDEvent -= IMassProcess_CMDEvent;
		ExperimentDatas.autoMisfire -= Experiment_autoMisfire;
	}

	public void ChangeUser(object sender, EventArgs e)
	{
		FormLogin form = new FormLogin();
		form.ShowDialog();
	}

	public void ManageUsers(object sender, EventArgs e)
	{
		FormUser form = new FormUser();
		form.Show();
	}

	public void LIMSTest(object sender, EventArgs e)
	{
		FormLIMS form = new FormLIMS();
		form.Show();
	}

	private void Detector_analogReceive(object sender, PointEventArgs e)
	{
		try
		{
			if (e.Index == 0)
			{
				toolZedgraph.GetCurrentParent().Invoke((MethodInvoker)delegate
				{
					toolZedgraph.Zedgraph.GraphPane.CurveList[0].Clear();
				});
			}
			double x = e.X;
			double y = e.Y * 100.0;
			toolZedgraph.AddPoint(e.X, y);
		}
		catch
		{
		}
	}

	private void Layout_Cascade(object sender, EventArgs e)
	{
		if (Shell is Form form)
		{
			form.LayoutMdi(MdiLayout.Cascade);
		}
	}

	private void Layout_Tile(object sender, EventArgs e)
	{
		if (Shell is Form form)
		{
			form.LayoutMdi(MdiLayout.TileHorizontal);
		}
	}

	private void Layout_Arrange(object sender, EventArgs e)
	{
		if (Shell is Form form)
		{
			form.LayoutMdi(MdiLayout.ArrangeIcons);
		}
	}

	private void Forms_CloseCurrent(object sender, EventArgs e)
	{
	}

	private void Forms_CloseAll(object sender, EventArgs e)
	{
	}

	private void VacuumOn_Click(object sender, EventArgs e)
	{
		if (!(PlasmaManager.plasma.ReadInterfaceTemperature < 18.0) || MessageBox.Show(Lang.Txt("AddiMass.Text33"), Lang.Txt("AddiMass.Text34"), MessageBoxButtons.OKCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button2) != DialogResult.Cancel)
		{
			Vacuum.Instance.NewVentOff();
			Thread.Sleep(1000);
			OnVacuum();
			Log.InsertLog(Lang.Txt("AddFormIonSource.UserAction"), Lang.Txt("iMass.ToolBar.VacuumOn.Text"), "", Severity.Info);
		}
	}

	private void VacuumOff_Click(object sender, EventArgs e)
	{
		if (MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.VaccumOff.Text"), Lang.Txt("iMass.MessageBox.Caption.VaccumOff.Tip.Text"), MessageBoxButtons.YesNo) == DialogResult.Yes)
		{
			OffVacuum();
			Log.InsertLog(Lang.Txt("AddFormIonSource.UserAction"), Lang.Txt("iMass.PlasmaOff.VaccumOff.Text"), "", Severity.Info);
		}
	}

	private void PlasmaOn_Click(object sender, EventArgs e)
	{
		if (taskOffPlasma != null && taskOffPlasma.IsAlive)
		{
			MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.WaitPlasmaOff"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
		}
		else if (Vacuum.vacumm.ReadMode < 2)
		{
			MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.VaccumOnAsk.Text"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
		}
		else if (PlasmaManager.plasma.StatusPlasma == 7)
		{
			MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.PlasmaIsOn.Text"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
		}
		else if (MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.PlasmaOn.Text"), Lang.Txt("iMass.MessageBox.Caption.PlasmaOn.Text"), MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.No)
		{
			DialogResult dr = MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.InXYZAxis.Text"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
			if (dr == DialogResult.Yes)
			{
				IsInXYZAxis = true;
			}
			else
			{
				IsInXYZAxis = false;
			}
			taskOnPlasma = new Thread(OnPlasma);
			taskOnPlasma.Start(false);
		}
	}

	private void PlasmaOff_Click(object sender, EventArgs e)
	{
		if (taskOffPlasma != null && taskOffPlasma.IsAlive)
		{
			MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.WaitPlasmaOff"));
		}
		else if (MessageBox.Show(Lang.Txt("iMass.MessageBox.Info.PlasmaOff.Text"), Lang.Txt("iMass.MessageBox.Caption.PlasmaOff.Text"), MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.No)
		{
			taskOffPlasma = new Thread(OffPlasma);
			taskOffPlasma.Start(false);
		}
	}

	private void OnVacuum()
	{
		Vacuum.Instance.NewVacuumOn();
	}

	private void OffVacuum()
	{
		Vacuum.Instance.NewVacuumOff();
	}

	private void OnPlasma(object bOut)
	{
		Log.InsertLog(Lang.Txt("AddFormIonSource.UserAction"), Lang.Txt("iMass.ToolBar.PlasmaOn.Text"), "", Severity.Info);
		CancelPlasma = false;
		PowerManager.Instance.MC3 = true;
		IgniteConfig igniteconfig = DataBase.Instance.GetIgniteConfig();
		if (igniteconfig == null)
		{
			igniteconfig = new IgniteConfig();
		}
		double PositionY = PlatManager.plat.ReadY / 100.0;
		double PositionX = PlatManager.plat.ReadX / 100.0;
		double PositionZ = PlatManager.plat.ReadZ / 100.0;
		if (IsInXYZAxis)
		{
			app.StatusControl.Progress(null, 10, Lang.Txt("AddiMass.Text35"));
			iMassProcess.Instance.PlasmaOn(2);
			Plasma.Instance.ResetPlat();
			Thread.Sleep(30000);
			app.StatusControl.Progress(null, 10, Lang.Txt("AddiMass.Text36"));
			PlatManager.plat.X = (float)PositionX;
			Plasma.Instance.SetXAxis();
			Thread.Sleep(5000);
			PlatManager.plat.Z = (float)PositionZ;
			Plasma.Instance.SetZAxis();
			Thread.Sleep(5000);
			PlatManager.plat.Y = (float)igniteconfig.IgniteY;
			Plasma.Instance.SetYAxis();
			Thread.Sleep(5000);
		}
		iMassProcess.Instance.PlasmaOn(3);
		PlasmaManager.plasma.Nebuliser = igniteconfig.NubelizerGas;
		Plasma.Instance.OpenNebulizerGas();
		PlasmaManager.plasma.Cool = igniteconfig.CoolGas;
		Plasma.Instance.OpenCoolGas();
		PlasmaManager.plasma.Auxiliary = igniteconfig.AuxiliaryGas;
		Plasma.Instance.OpenAuxiliaryGas();
		PlasmaManager.plasma.Appendix1 = 0.05;
		Plasma.Instance.OpenAppendixGas();
		PlatManager.injector.Perist = (int)igniteconfig.NormalSpeed * 10;
		Plasma.Instance.PeristalticPumpOnNormal();
		app.StatusControl.Progress(null, 10, Lang.Txt("iMass.Status.CarrierOpen.Text"));
		int count = 0;
		while (!CancelPlasma)
		{
			Thread.Sleep(2000);
			if (count++ >= 5)
			{
				break;
			}
		}
		if (CancelPlasma)
		{
			Plasma.Instance.PlasmaOff();
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			app.StatusControl.Progress(null, 0, Lang.Txt("iMass.Status.CancelPlasma.Text"));
			iMassProcess.Instance.PlasmaOn(1);
			return;
		}
		Plasma.Instance.PlasmaOn();
		app.StatusControl.Progress(null, 20, Lang.Txt("iMass.PlasmaOn.Text"));
		Thread.Sleep(100);
		PlasmaManager.plasma.Cool = igniteconfig.CoolGas;
		Plasma.Instance.OpenCoolGas();
		PlasmaManager.plasma.Auxiliary = igniteconfig.AuxiliaryGas;
		Plasma.Instance.OpenAuxiliaryGas();
		count = 0;
		while (!CancelPlasma)
		{
			Thread.Sleep(2000);
			if (count++ >= 5)
			{
				break;
			}
		}
		if (CancelPlasma)
		{
			Plasma.Instance.PlasmaOff();
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			app.StatusControl.Progress(null, 0, Lang.Txt("iMass.Status.CancelPlasma.Text"));
			iMassProcess.Instance.PlasmaOn(1);
			return;
		}
		count = 0;
		bool bIgnitePowerSet = false;
		bool bTunePowerSet = false;
		int OnPlasmaCount = 0;
		while (!CancelPlasma)
		{
			Thread.Sleep(2000);
			count++;
			if (count > 60)
			{
				PlatManager.plat.Y = (float)PositionY;
				Plasma.Instance.SetYAxis();
				iMassProcess.Instance.PlasmaOn(1);
				return;
			}
			if (PlasmaManager.plasma.StatusPlasma == 7)
			{
				OnPlasmaCount++;
				if (OnPlasmaCount > 2)
				{
					PlasmaManager.plasma.StatusOldPlasma = 7;
					app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process.Success.Text"));
					break;
				}
			}
			else if (PlasmaManager.plasma.StatusPlasma == 0)
			{
				PlatManager.plat.Y = (float)PositionY;
				Plasma.Instance.SetYAxis();
				app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOn.Process.Failure.Text"));
				iMassProcess.Instance.PlasmaOn(1);
				return;
			}
			switch (PlasmaManager.plasma.StatusPlasma)
			{
			case 1:
				app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process1.Text"));
				app.StatusControl.Progress(null, 0, Lang.Txt("iMass.Status.CancelPlasma.Text"));
				return;
			case 2:
				app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process2.Text"));
				break;
			case 3:
				app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process3.Text"));
				break;
			case 4:
				app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process4.Text"));
				if (!bTunePowerSet)
				{
					Plasma.Instance.SetPower(igniteconfig.Power1);
					bTunePowerSet = true;
				}
				break;
			case 5:
				app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process5.Text"));
				if (!bIgnitePowerSet)
				{
					Plasma.Instance.SetPower(igniteconfig.Power);
					bIgnitePowerSet = true;
				}
				break;
			case 6:
				app.StatusControl.Progress(null, 50, Lang.Txt("iMass.PlasmaOn.Process6.Text"));
				break;
			}
		}
		if (CancelPlasma)
		{
			Plasma.Instance.PlasmaOff();
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			app.StatusControl.Progress(null, 0, Lang.Txt("iMass.Status.CancelPlasma.Text"));
			iMassProcess.Instance.PlasmaOn(1);
			return;
		}
		app.StatusControl.Progress(null, 80, Lang.Txt("iMass.PlasmaOn.SetSamplingDepth.Text"));
		PlatManager.plat.Y = (float)igniteconfig.Y;
		Plasma.Instance.SetYAxis();
		count = 0;
		while (!CancelPlasma)
		{
			Thread.Sleep(2000);
			if (count++ >= 3)
			{
				break;
			}
		}
		if (CancelPlasma)
		{
			Plasma.Instance.PlasmaOff();
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			app.StatusControl.Progress(null, 0, Lang.Txt("iMass.Status.CancelPlasma.Text"));
			iMassProcess.Instance.PlasmaOn(1);
			return;
		}
		app.StatusControl.Progress(null, 90, Lang.Txt("iMass.PlasmaOn.OpenValve.Text"));
		Vacuum.Instance.NewVacuumOn();
		Thread.Sleep(8000);
		int count2 = 0;
		while (!CancelPlasma)
		{
			Thread.Sleep(4000);
			count2++;
			if (count2 > 10)
			{
				app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOn.VaccumError.Text"));
				PlatManager.plat.Y = (float)PositionY;
				Plasma.Instance.SetYAxis();
				Plasma.Instance.PlasmaOff();
				iMassProcess.Instance.PlasmaOn(1);
				return;
			}
			if (Vacuum.vacumm.ReadMode > 2)
			{
				if (Vacuum.vacumm.ReadMode == 5)
				{
					PlatManager.plat.Y = (float)PositionY;
					Plasma.Instance.SetYAxis();
					Thread.Sleep(12000);
					if (Vacuum.vacumm.ReadMode == 5)
					{
						Plasma.Instance.OpenShield();
						LensManager.Instance.X2_SetAll();
					}
					app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOn.Over.Text"));
					break;
				}
				if (PlasmaManager.plasma.StatusPlasma == 0)
				{
					PlatManager.plat.Y = (float)PositionY;
					Plasma.Instance.SetYAxis();
					iMassProcess.Instance.PlasmaOn(1);
					return;
				}
				continue;
			}
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOn.VaccumFailure.Text"));
			iMassProcess.Instance.PlasmaOn(1);
			return;
		}
		if (CancelPlasma)
		{
			Vacuum.Instance.NewVacuumOff();
			app.StatusControl.Progress(null, 10, Lang.Txt("iMass.PlasmaOn.CloseVaccum.Text"));
			for (int i = 0; i < 9; i++)
			{
				Thread.Sleep(2000);
				app.StatusControl.Progress(null, (i + 1) * 10, Lang.Txt("iMass.PlasmaOn.CloseVaccum.Text"));
			}
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			Plasma.Instance.PlasmaOff();
			LensManager.Instance.CloseLens();
			app.StatusControl.Progress(null, 0, Lang.Txt("iMass.Status.CancelPlasma.Text"));
			iMassProcess.Instance.PlasmaOn(1);
		}
		else
		{
			PlatManager.plat.Y = (float)PositionY;
			Plasma.Instance.SetYAxis();
			if (!(bool)bOut && Vacuum.vacumm.ReadMode == 5)
			{
				MessageBox.Show(Lang.Txt("AddiMass.Text37"));
			}
			iMassProcess.Instance.PlasmaOn(0);
		}
	}

	private void OffPlasma(object bOut)
	{
		if (taskOnPlasma != null && taskOnPlasma.IsAlive)
		{
			CancelPlasma = true;
			return;
		}
		if (!(bool)bOut && Detector.Instance.ExperimentState != -1)
		{
			if (Detector.Instance.ExperimentState == 5)
			{
				MessageBox.Show(Lang.Txt("AddiMass.Text38"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
				return;
			}
			if (MessageBox.Show(Lang.Txt("AddiMass.Text39"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.YesNo) != DialogResult.Yes)
			{
				return;
			}
			Detector.Instance.ExperimentStateChange(Detector.Instance.ExperimentState);
		}
		Log.InsertLog(Lang.Txt("AddFormIonSource.UserAction"), Lang.Txt("iMass.ToolBar.PlasmaOff.Text"), "", Severity.Info);
		if (PlasmaManager.plasma.StatusPlasma != 7)
		{
			return;
		}
		app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOff.Prepare.Text"));
		iMassProcess.Instance.PlasmaOff(2);
		LensManager.Instance.CloseEtp();
		Thread.Sleep(1);
		RFManager.rfs.HVP = 0f;
		RFManager.rfs.HVN = 0f;
		RFManager.rfs.Gate = 0f;
		if (Detector.detectorConfig.IsNewDetector)
		{
			Detector.Instance.msDetectorRF.SetHVPNew();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetHVNNew();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetGateNew();
		}
		else
		{
			Detector.Instance.msDetectorRF.SetHVP();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetHVN();
			Thread.Sleep(1);
			Detector.Instance.msDetectorRF.SetGate();
		}
		LensManager.Instance.CloseLens();
		Thread.Sleep(5000);
		iMassProcess.Instance.PlasmaOff(2);
		if (Vacuum.vacumm.ReadMode > 2)
		{
			Vacuum.Instance.NewVacuumOff();
		}
		Thread.Sleep(5000);
		int count = 0;
		while (count < 4)
		{
			iMassProcess.Instance.PlasmaOff(2);
			app.StatusControl.Progress(null, 10, Lang.Txt("iMass.PlasmaOff.VaccumOff.Text"));
			Thread.Sleep(5000);
			count++;
			if (Vacuum.vacumm.ReadMode == 2)
			{
				break;
			}
		}
		if (Vacuum.vacumm.ReadMode >= 4)
		{
			iMassProcess.Instance.PlasmaOff(1);
			return;
		}
		if (PlasmaManager.plasma.StatusPlasma != 0)
		{
			Plasma.Instance.PlasmaOff();
		}
		PlasmaManager.plasma.StatusOldPlasma = 0;
		Thread.Sleep(1000);
		app.StatusControl.Progress(null, 30, Lang.Txt("iMass.PlasmaOff.breeze.Text"));
		PlasmaManager.plasma.Cool = 15.0;
		Plasma.Instance.OpenCoolGas();
		PlasmaManager.plasma.Auxiliary = 0.8;
		Plasma.Instance.OpenAuxiliaryGas();
		iMassProcess.Instance.PlasmaOff(2);
		for (int i = 0; i < 30; i++)
		{
			app.StatusControl.Progress(null, 40 + i, string.Format(Lang.Txt("iMass.PlasmaOff.Cool.Text") + "{0}s..", (30 - i) * 2));
			Thread.Sleep(2000);
		}
		Plasma.Instance.CloseCoolGas();
		Plasma.Instance.CloseAppendixGas();
		Plasma.Instance.CloseAuxiliaryGas();
		Plasma.Instance.CloseCollision();
		Plasma.Instance.CloseCollision2();
		app.StatusControl.Progress(null, 0, Lang.Txt("iMass.PlasmaOff.Finish.Text"));
		CancelPlasma = false;
		if (!(bool)bOut)
		{
			MessageBox.Show(Lang.Txt("AddiMass.Text40"), Lang.Txt("AddFormIonSource.Warn"), MessageBoxButtons.OK);
		}
		iMassProcess.Instance.PlasmaOff(0);
	}

	private void Tick_InstrumentStatus()
	{
		try
		{
			tickCount++;
			if (tickCount == 5)
			{
				tickCount = 0;
				int disconnectedModules = 0;
				StringBuilder sb = new StringBuilder();
				if (!CheckPlatformConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Plat.Text"));
				}
				if (!CheckIcpConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.ICP.Text"));
				}
				if (!CheckVacuumConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Vaccum.Text"));
				}
				if (!CheckPeristicConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Peristic.Text"));
				}
				if (!CheckMfcConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.MFC.Text"));
				}
				if (!CheckLensConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Lens.Text"));
				}
				if (!CheckPowerConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Power.Text"));
				}
				if (!CheckQuadConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Quad.Text"));
				}
				if (!CheckCollisionConnection())
				{
					disconnectedModules++;
					sb.AppendLine(Lang.Txt("iMass.Connect.Error.Collision.Text"));
				}
				switch (disconnectedModules)
				{
				case 0:
					connPanel.Image = connOK;
					connPanel.ToolTipText = Lang.Txt("iMass.Connect.Success.Text");
					break;
				case 9:
					connPanel.Image = connError;
					connPanel.ToolTipText = Lang.Txt("iMass.Connect.AllError.Text");
					break;
				default:
					connPanel.Image = connWarn;
					connPanel.ToolTipText = sb.ToString();
					break;
				}
			}
			UpdateVacuums();
			UpdatePlasmas(log: false);
			UpdatePlat();
			CheckPlasmaOn();
			CheckAccidentPlasmaOff();
		}
		catch (Exception ex)
		{
			Log.InsertLog(Lang.Txt("AddiMass.Text43"), "", ex.Message, Severity.Error);
			Console.WriteLine(Lang.Txt("AddiMass.Text43") + "：" + ex.Message);
		}
	}

	private void CheckPlasmaOn()
	{
		try
		{
			byte err = (byte)PlasmaManager.plasma.ReadError;
			if ((err & 0x80) >> 7 == 1)
			{
				if (!_ifErr)
				{
					string mesg = Lang.Txt("AddiMass.Text44");
					if ((err & 1) == 1)
					{
						mesg += Lang.Txt("AddiMass.Text44");
					}
					if ((err & 2) >> 1 == 1)
					{
						mesg = mesg + Lang.Txt("FormIonSource.IgniteInterrupt.Text") + ";";
					}
					if ((err & 4) >> 2 == 1)
					{
						mesg = mesg + Lang.Txt("AddiMass.Text46") + ";";
					}
					if ((err & 8) >> 3 == 1)
					{
						mesg += Lang.Txt("AddiMass.Text47");
					}
					if ((err & 0x10) >> 4 == 1)
					{
						mesg += Lang.Txt("AddiMass.Text48");
					}
					if ((err & 0x20) >> 5 == 1)
					{
						mesg += Lang.Txt("AddiMass.Text49");
					}
					if ((err & 0x40) >> 6 == 1)
					{
						mesg += Lang.Txt("AddiMass.Text50");
					}
					Log.InsertLog(Lang.Txt("AddiMass.Text51"), mesg, "", Severity.Warning);
					PlasmaManager.recordCanData.SaveCanData();
					_ifErr = true;
				}
			}
			else
			{
				_ifErr = false;
			}
		}
		catch (Exception ex)
		{
			Log.InsertLog("CheckPlasmaOn", "", ex.Message, Severity.Error);
			Console.WriteLine("CheckPlasmaOn:" + ex.Message);
		}
	}

	private bool CheckCollisionConnection()
	{
		if (Collision.collision.StatusOnline)
		{
			Collision.collision.StatusOnline = false;
			Collision.collision.StatusConnected = true;
			return true;
		}
		Collision.collision.StatusConnected = false;
		return false;
	}

	private bool CheckPlatformConnection()
	{
		if (PlatManager.plat.StatusOnline)
		{
			PlatManager.plat.StatusOnline = false;
			PlatManager.plat.StatusConnected = true;
			return true;
		}
		PlatManager.plat.StatusConnected = false;
		return false;
	}

	private bool CheckIcpConnection()
	{
		if (PlasmaManager.plasma.StatusOnline)
		{
			PlasmaManager.plasma.StatusOnline = false;
			PlasmaManager.plasma.StatusConnected = true;
			return true;
		}
		PlasmaManager.plasma.StatusConnected = false;
		return false;
	}

	private bool CheckVacuumConnection()
	{
		if (Vacuum.vacumm.StatusOnline)
		{
			Vacuum.vacumm.StatusOnline = false;
			Vacuum.vacumm.StatusConnected = true;
			return true;
		}
		Vacuum.vacumm.StatusConnected = false;
		return false;
	}

	private bool CheckPeristicConnection()
	{
		if (PlatManager.injector.StatusOnline)
		{
			PlatManager.injector.StatusOnline = false;
			PlatManager.injector.StatusConnected = true;
			return true;
		}
		PlatManager.injector.StatusConnected = false;
		return false;
	}

	private bool CheckMfcConnection()
	{
		if (PlasmaManager.mfcStatus.StatusOnline)
		{
			PlasmaManager.mfcStatus.StatusOnline = false;
			PlasmaManager.mfcStatus.StatusConnected = true;
			return true;
		}
		PlasmaManager.mfcStatus.StatusConnected = false;
		return false;
	}

	private bool CheckLensConnection()
	{
		if (LensManager.x2lens.StatusOnline)
		{
			LensManager.x2lens.StatusOnline = false;
			LensManager.x2lens.StatusConnected = true;
			return true;
		}
		LensManager.x2lens.StatusConnected = false;
		return false;
	}

	private bool CheckPowerConnection()
	{
		if (PowerManager.powerStatus.StatusOnline)
		{
			PowerManager.powerStatus.StatusOnline = false;
			PowerManager.powerStatus.StatusConnected = true;
			return true;
		}
		PowerManager.powerStatus.StatusConnected = false;
		return false;
	}

	private bool CheckQuadConnection()
	{
		if (QuadMonitor.quad.StatusOnline)
		{
			QuadMonitor.quad.StatusOnline = false;
			QuadMonitor.quad.StatusConnected = true;
			return true;
		}
		QuadMonitor.quad.StatusConnected = false;
		return false;
	}

	private bool GetBit(ushort value, int index)
	{
		int tmp = 1 << index;
		return (value & tmp) == tmp;
	}

	private bool GetBit(uint value, int index)
	{
		int tmp = 1 << index;
		return (value & tmp) == tmp;
	}

	private void UpdateVacuums()
	{
		try
		{
			string toolTipText = "";
			Color foreColor = Color.Black;
			if (!GetBit(Vacuum.turbo1.PZD1, 8) || !GetBit(Vacuum.turbo2.PZD1, 8))
			{
				toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Unconnected.Text");
				foreColor = Color.Red;
			}
			else
			{
				bool bContinue = true;
				if (bContinue && (GetBit(Vacuum.turbo1.PZD1, 12) || GetBit(Vacuum.turbo2.PZD1, 12)))
				{
					toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Communication.Error.Text");
					foreColor = Color.Red;
					bContinue = false;
				}
				if (bContinue && (GetBit(Vacuum.turbo1.PZD1, 7) || GetBit(Vacuum.turbo2.PZD1, 7)))
				{
					toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Temperature.Error.Text");
					foreColor = Color.DodgerBlue;
					bContinue = false;
				}
				if (bContinue && (GetBit(Vacuum.turbo1.PZD1, 3) || GetBit(Vacuum.turbo2.PZD1, 3)))
				{
					toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Temperature.Error.Text");
					foreColor = Color.Red;
					bContinue = false;
				}
				if (bContinue)
				{
					if (GetBit(Vacuum.turbo1.PZD1, 4) || GetBit(Vacuum.turbo2.PZD1, 4))
					{
						toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Accelerate.Text");
					}
					if (GetBit(Vacuum.turbo1.PZD1, 5) || GetBit(Vacuum.turbo2.PZD1, 5))
					{
						toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Decelerate.Text");
					}
					if (GetBit(Vacuum.turbo1.PZD1, 10) && GetBit(Vacuum.turbo2.PZD1, 10))
					{
						toolTipText = Lang.Txt("iMass.Vaccum.Turbo.Normal.Text");
					}
					foreColor = Color.Black;
				}
			}
			turboValues.Caption = $"{Vacuum.turbo1.PZD2}Hz,{Vacuum.turbo2.PZD2}Hz ";
			turboValues.ToolTipText = toolTipText;
			turboValues.Color = foreColor;
			int status1 = Vacuum.vacumm.ReadVacuumStatus[0];
			int status2 = Vacuum.vacumm.ReadVacuumStatus[1];
			int status3 = Vacuum.vacumm.ReadVacuumStatus[2];
			double quadVacuum = Vacuum.vacumm.ReadVacuum[2];
			double lensVacuum = Vacuum.vacumm.ReadVacuum[1];
			double intVacuum = Vacuum.vacumm.ReadVacuum[0];
			double GasAr = PlasmaManager.plasma.ReadGasAr;
			StringBuilder sb = new StringBuilder();
			sb.Append(string.Format(Lang.Txt("iMass.Vaccum.GasAr.Value.Text") + "{0:0.0} MPa", GasAr));
			sb.Append((status1 == 1) ? string.Format(Lang.Txt("iMass.Vaccum.Int.Value.Text") + "{0:0.00e0}Pa ", intVacuum) : (Lang.Txt("iMass.Vaccum.Int.Value.Text") + "--- "));
			if (Vacuum.Instance.PiraniType == 1)
			{
				sb.Append((status2 == 1) ? string.Format(Lang.Txt("iMass.Vaccum.Lens.Value.Text") + "{0:0.00e0}Pa ", lensVacuum) : (Lang.Txt("iMass.Vaccum.Lens.Value.Text") + "--- "));
			}
			sb.Append(((Vacuum.Instance.VacuumType == 2 && status3 == 7) || (Vacuum.Instance.VacuumType == 1 && status3 == 3) || Vacuum.Instance.VacuumType == 3) ? string.Format(Lang.Txt("iMass.Vaccum.Quad.Value.Text") + "{0:0.00e0}Pa ", quadVacuum) : (Lang.Txt("iMass.Vaccum.Quad.Value.Text") + "---"));
			if (!GetBit((ushort)status3, 0))
			{
				toolTipText = sb.ToString();
			}
			else if (!GetBit((ushort)status3, 1))
			{
				toolTipText = Lang.Txt("iMass.Vaccum.Quad.State.HVOFF.Text");
			}
			else if (!GetBit((ushort)status3, 2))
			{
				toolTipText = Lang.Txt("iMass.Vaccum.Quad.State.IgniteOff.Text");
			}
			else
			{
				toolTipText = sb.ToString();
			}
			vacuumValues.Caption = sb.ToString();
			vacuumValues.ToolTipText = sb.ToString();
			vacuumValues.Size = 10000;
			vacuumValues.Width = 500;
			string text = "";
			switch (Vacuum.vacumm.ReadMode)
			{
			case 0:
				text = Lang.Txt("iMass.VaccumOn.State.Stay.Text");
				break;
			case 1:
				text = Lang.Txt("iMass.VaccumOn.State.Ing.Text");
				break;
			case 2:
				text = Lang.Txt("iMass.VaccumOn.State.Staying.Text");
				break;
			case 3:
				text = Lang.Txt("iMass.VaccumOn.State.Off.Text");
				break;
			case 4:
				text = Lang.Txt("iMass.VaccumOn.State.ValveOpen.Text");
				break;
			case 5:
				text = Lang.Txt("iMass.VaccumOn.State.Ready.Text");
				valveEvents[0].Set();
				break;
			}
			vacuumStatus.Caption = text;
			vacuumStatus.ToolTipText = text;
		}
		catch (Exception ex)
		{
			Log.InsertLog(Lang.Txt("AddiMass.Text52"), Lang.Txt("AddiMass.Text53"), ex.Message, Severity.Error);
			Console.WriteLine(Lang.Txt("AddiMass.Text54") + ex.Message);
		}
	}

	private void UpdatePlasmas(bool log)
	{
		try
		{
			int Error = 1;
			StringBuilder sb = new StringBuilder();
			if (PlasmaManager.plasma.StatusDoor == Error)
			{
				sb.Append(Lang.Txt("iMass.Plasma.StatusDoor.Error.Text"));
				if (log)
				{
					Log.InsertLog(Lang.Txt("iMass.Plasma.PlasmaError.Text"), Lang.Txt("iMass.Plasma.StatusDoor.Error.Text"), "", Severity.Error);
				}
			}
			if ((PlasmaManager.plasma.StatusCoolWater == Error || PlasmaManager.plasma.ReadWater < 10.0) && PlasmaManager.plasma.StatusPlasma != 0)
			{
				sb.Append(Lang.Txt("iMass.Plasma.StatusCoolWater.Text"));
				if (log)
				{
					Log.InsertLog(Lang.Txt("iMass.Plasma.PlasmaError.Text"), Lang.Txt("iMass.Plasma.StatusCoolWater.Text"), "", Severity.Error);
				}
			}
			if (PlasmaManager.plasma.StatusArSwitch == Error && PlasmaManager.plasma.StatusPlasma != 0)
			{
				sb.Append(Lang.Txt("iMass.Plasma.StatusArSwitch.Text"));
				if (log)
				{
					Log.InsertLog(Lang.Txt("iMass.Plasma.PlasmaError.Text"), Lang.Txt("iMass.Plasma.StatusArSwitch.Text"), "", Severity.Error);
				}
			}
			if (PlasmaManager.plasma.ReadTemperatureWind >= 40.0)
			{
				sb.Append(Lang.Txt("iMass.Plasma.StatusWindTemperature.Text"));
				if (log)
				{
					Log.InsertLog(Lang.Txt("iMass.Plasma.PlasmaError.Text"), Lang.Txt("iMass.Plasma.StatusWindTemperature.Text"), "", Severity.Error);
				}
			}
			plasmaWarns.Caption = sb.ToString();
			plasmaWarns.Color = Color.Red;
			if (PlasmaManager.plasma.StatusPlasma == 7)
			{
				plasmaPanel.Image = plasmaOn;
				plasmaPanel.ToolTipText = Lang.Txt("iMass.Plasma.On.ToolTip.Text");
			}
			else if (PlasmaManager.plasma.StatusPlasma == 0)
			{
				plasmaPanel.Image = plasmaOff;
				plasmaPanel.ToolTipText = Lang.Txt("iMass.Plasma.Off.ToolTip.Text");
			}
		}
		catch (Exception ex)
		{
			Log.InsertLog(Lang.Txt("AddiMass.Text43"), "", ex.Message, Severity.Error);
			Console.WriteLine(Lang.Txt("AddiMass.Text55") + ex.Message);
		}
	}

	private void UpdatePlat()
	{
		uint warn = PlatManager.plat.ReadWarn;
		StringBuilder sbWarn = new StringBuilder();
		if (GetBit(warn, 1))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.XUnconnected.Text"));
		}
		if (GetBit(warn, 2))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.YUnconnected.Text"));
		}
		if (GetBit(warn, 3))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.ZUnconnected.Text"));
		}
		if (GetBit(warn, 4))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.PumpHotProtect.Text"));
		}
		if (GetBit(warn, 5))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.XHotProtected.Text"));
		}
		if (GetBit(warn, 6))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.YHotProtected.Text"));
		}
		if (GetBit(warn, 7))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.ZHotProtected.Text"));
		}
		if (GetBit(warn, 13))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.XZeroProtected.Text"));
		}
		if (GetBit(warn, 14))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.YZeroProtected.Text"));
		}
		if (GetBit(warn, 15))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.ZZeroProtected.Text"));
		}
		if (GetBit(warn, 17))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.XReset.Text"));
		}
		if (GetBit(warn, 18))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.YReset.Text"));
		}
		if (GetBit(warn, 19))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.ZReset.Text"));
		}
		if (GetBit(warn, 21))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.XCalibration.Text"));
		}
		if (GetBit(warn, 22))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.YCalibration.Text"));
		}
		if (GetBit(warn, 23))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.ZCalibration.Text"));
		}
		if (GetBit(warn, 25))
		{
		}
		if (GetBit(warn, 26))
		{
		}
		if (GetBit(warn, 27))
		{
		}
		if (GetBit(warn, 29))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.XFarProtected.Text"));
		}
		if (GetBit(warn, 30))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.YFarProtected.Text"));
		}
		if (GetBit(warn, 31))
		{
			sbWarn.Append(Lang.Txt("FormPlat.Error.ZFarProtected.Text"));
		}
		platWarns.Caption = sbWarn.ToString();
		platWarns.ToolTipText = sbWarn.ToString();
		platWarns.Color = Color.Red;
	}

	private void CheckAccidentPlasmaOff()
	{
		try
		{
			int Error = 1;
			int err = PlasmaManager.plasma.ReadError;
			if ((err & 0x80) >> 7 == 1 && PlasmaManager.plasma.StatusOldPlasma == 7 && PlasmaManager.plasma.StatusPlasma == 1)
			{
				PlasmaManager.plasma.StatusOldPlasma = 1;
				string ss = "";
				if (PlasmaManager.plasma.StatusCoolWater == Error)
				{
					ss = ss + Lang.Txt("iMass.Plasma.StatusCoolWater.Text") + "\n ";
				}
				if (PlasmaManager.plasma.ReadNebuliser <= 0.6)
				{
					ss = ss + Lang.Txt("iMass.Plasma.StatusArSwitch.Text") + "\n ";
				}
				if (PlasmaManager.plasma.ReadTemperatureWind >= 40.0)
				{
					ss = ss + Lang.Txt("iMass.Plasma.StatusWindTemperature.Text") + "\n ";
				}
				if (PlasmaManager.plasma.ReadInterfaceTemperature >= 45.0)
				{
					ss = ss + Lang.Txt("iMass.Plasma.StatusInterfaceTemperature.Text") + "\n ";
				}
				if (PlasmaManager.plasma.ReadWater < 10.0)
				{
					ss += Lang.Txt("iMass.Plasma.StatusWater.Text\n ");
				}
				ss = ss + Lang.Txt("FormIonSource.ReflectionPower.Text") + "：" + PlasmaManager.plasma.ReadReflection.ToString("F1") + "\n ";
				ss = ss + Lang.Txt("FormIonSource.ForwardPower.Text") + "：" + PlasmaManager.plasma.ReadForward.ToString("F1") + "\n";
				ss = ss + Lang.Txt("FormPlat.GroupBox.Pump.Text") + "：" + PlatManager.injector.ReadPerist + " \n";
				if (PlasmaManager.plasma.StatusCoolWater == Error)
				{
					ss = ss + Lang.Txt("AddiMass.Text56") + "\n ";
				}
				if (PlasmaManager.plasma.StatusCoolSwitch == Error)
				{
					ss = ss + Lang.Txt("AddiMass.Text57") + "\n ";
				}
				if (PlasmaManager.plasma.StatusArSwitch == Error)
				{
					ss = ss + Lang.Txt("AddiMass.Text58") + "\n ";
				}
				if (PlasmaManager.plasma.StatusDoor == Error)
				{
					ss = ss + Lang.Txt("AddiMass.Text59") + "\n ";
				}
				if (PlasmaManager.plasma.StatusLight == Error)
				{
					ss = ss + Lang.Txt("AddiMass.Text60") + "\n ";
				}
				if (PlasmaManager.plasma.StatusPowerSwitch == Error)
				{
					ss = ss + Lang.Txt("AddiMass.Text61") + "\n ";
				}
				if (PlasmaManager.plasma.StatusNegative24 == Error)
				{
					ss = ss + "-" + Lang.Txt("AddiMass.Text62") + "\n ";
				}
				if (PlasmaManager.plasma.StatusPositive24 == Error)
				{
					ss = ss + "+ " + Lang.Txt("AddiMass.Text62") + "\n ";
				}
				if (PlasmaManager.plasma.StatusCanAndGasFlowInLock == Error)
				{
					ss = ss + "CanAndGasFlowInLock" + Lang.Txt("AddiMass.Text63") + "\n ";
				}
				if (PlasmaManager.plasma.StatusRFEn == 0)
				{
					ss = ss + "RFEN" + Lang.Txt("AddiMass.Text63") + "\n ";
				}
				if (PlasmaManager.plasma.StatusCLRIntLock == Error)
				{
					ss = ss + "CLRIntLock" + Lang.Txt("AddiMass.Text63") + "\n ";
				}
				if (PlasmaManager.plasma.StatusLightPrOn == 0)
				{
					ss = ss + "LightPrOn" + Lang.Txt("AddiMass.Text63") + "\n ";
				}
				if (PlasmaManager.plasma.RFFailure == Error)
				{
					ss = ss + "RFFailure" + Lang.Txt("AddiMass.Text63") + "\n ";
				}
				Log.InsertLog(Lang.Txt("AddiMass.Text63"), Lang.Txt("FormIonSource.GroupBox.Status.Text"), ss, Severity.Error);
			}
		}
		catch (Exception ex)
		{
			Log.InsertLog(Lang.Txt("AddiMass.Text64"), "", ex.Message, Severity.Error);
		}
	}

	private void PlasmaStatus_AbnormalChecked(object sender, EventArgs e)
	{
		UpdatePlasmas(log: true);
	}

	private void Vacuum_errorEvent(object sender, EventArgs e)
	{
		bool ignoreVacuumStatus = true;
		int flag = Convert.ToInt32(sender);
		valveEvents[1].Set();
		string category = Lang.Txt("iMass.Vaccum.Process.Text");
		string content = "";
		string detail = "";
		switch (flag)
		{
		case 1:
			content = Lang.Txt("iMass.Vaccum.Process.Error1.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error1.Detail.Text");
			break;
		case 2:
			content = Lang.Txt("iMass.Vaccum.Process.Error2.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error2.Detail.Text");
			break;
		case 3:
			content = Lang.Txt("iMass.Vaccum.Process.Error3.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error3.Detail.Text");
			break;
		case 5:
			content = Lang.Txt("iMass.Vaccum.Process.Error5.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error5.Detail.Text");
			break;
		case 6:
			content = Lang.Txt("iMass.Vaccum.Process.Error6.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error6.Detail.Text");
			break;
		case 7:
			content = Lang.Txt("iMass.Vaccum.Process.Error7.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error7.Detail.Text");
			break;
		case 8:
			content = Lang.Txt("iMass.Vaccum.Process.Error8.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error8.Detail.Text");
			break;
		case 101:
			content = Lang.Txt("iMass.Vaccum.Process.Error101.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error101.Detail.Text");
			break;
		case 102:
			content = Lang.Txt("iMass.Vaccum.Process.Error102.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error102.Detail.Text");
			break;
		case 103:
			content = Lang.Txt("iMass.Vaccum.Process.Error103.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error103.Detail.Text");
			break;
		case 104:
			content = Lang.Txt("iMass.Vaccum.Process.Error104.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error104.Detail.Text");
			break;
		case 105:
			content = Lang.Txt("iMass.Vaccum.Process.Error105.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error105.Detail.Text");
			break;
		case 106:
			content = Lang.Txt("iMass.Vaccum.Process.Error106.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error106.Detail.Text");
			break;
		case 107:
			content = Lang.Txt("iMass.Vaccum.Process.Error107.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error107.Detail.Text");
			break;
		case 201:
			content = Lang.Txt("iMass.Vaccum.Process.Error201.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error201.Detail.Text");
			break;
		case 202:
			content = Lang.Txt("iMass.Vaccum.Process.Error202.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error202.Detail.Text");
			break;
		case 203:
			content = Lang.Txt("iMass.Vaccum.Process.Error203.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error203.Detail.Text");
			break;
		case 204:
			content = Lang.Txt("iMass.Vaccum.Process.Error204.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error204.Detail.Text");
			break;
		case 109:
			content = Lang.Txt("iMass.Vaccum.Process.Error109.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error109.Detail.Text");
			break;
		case 400:
			content = Lang.Txt("iMass.Vaccum.Process.Error400.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error400.Detail.Text");
			break;
		case 401:
			content = Lang.Txt("iMass.Vaccum.Process.Error401.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error401.Detail.Text");
			ignoreVacuumStatus = false;
			break;
		case 402:
			content = Lang.Txt("iMass.Vaccum.Process.Error402.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error402.Detail.Text");
			ignoreVacuumStatus = false;
			break;
		case 403:
			content = Lang.Txt("iMass.Vaccum.Process.Error403.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error403.Detail.Text");
			ignoreVacuumStatus = false;
			break;
		case 501:
			content = Lang.Txt("iMass.Vaccum.Process.Error501.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error501.Detail.Text");
			break;
		case 502:
			content = Lang.Txt("iMass.Vaccum.Process.Error502.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error502.Detail.Text");
			break;
		case 503:
			content = Lang.Txt("iMass.Vaccum.Process.Error503.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error503.Detail.Text");
			break;
		case 504:
			content = Lang.Txt("iMass.Vaccum.Process.Error504.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error504.Detail.Text");
			break;
		case 505:
			content = Lang.Txt("iMass.Vaccum.Process.Error505.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error505.Detail.Text");
			break;
		case 506:
			content = Lang.Txt("iMass.Vaccum.Process.Error506.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error506.Detail.Text");
			break;
		case 507:
			content = Lang.Txt("iMass.Vaccum.Process.Error507.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error507.Detail.Text");
			break;
		case 508:
			content = Lang.Txt("iMass.Vaccum.Process.Error508.Content.Text");
			detail = Lang.Txt("iMass.Vaccum.Process.Error508.Detail.Text");
			break;
		}
		Log.InsertLog(category, content, detail, Severity.Error);
		if (ignoreVacuumStatus)
		{
			MessageBox.Show(detail, Lang.Txt("iMass.Vaccum.Process.Text"), MessageBoxButtons.OK);
		}
		ignoreVacuumStatus = false;
		if (VacuumErrorOldState != flag)
		{
			VacuumErrorOldState = flag;
		}
	}

	private void PlatManager_errorEvent(object sender, EventArgs e)
	{
		uint error = PlatManager.plat.ReadError;
		string content = "";
		switch (error)
		{
		case 101u:
			content = Lang.Txt("iMass.Plat.Process.Error101.Content.Text");
			break;
		case 102u:
			content = Lang.Txt("iMass.Plat.Process.Error102.Content.Text");
			break;
		case 103u:
			content = Lang.Txt("iMass.Plat.Process.Error103.Content.Text");
			break;
		case 104u:
			content = Lang.Txt("iMass.Plat.Process.Error104.Content.Text");
			break;
		case 201u:
			content = Lang.Txt("iMass.Plat.Process.Error201.Content.Text");
			break;
		case 202u:
			content = Lang.Txt("iMass.Plat.Process.Error202.Content.Text");
			break;
		case 203u:
			content = Lang.Txt("iMass.Plat.Process.Error203.Content.Text");
			break;
		case 204u:
			content = Lang.Txt("iMass.Plat.Process.Error204.Content.Text");
			break;
		case 301u:
			content = Lang.Txt("iMass.Plat.Process.Error301.Content.Text");
			break;
		case 302u:
			content = Lang.Txt("iMass.Plat.Process.Error302.Content.Text");
			break;
		case 303u:
			content = Lang.Txt("iMass.Plat.Process.Error303.Content.Text");
			break;
		case 304u:
			content = Lang.Txt("iMass.Plat.Process.Error304.Content.Text");
			break;
		}
		if (content != "")
		{
			Log.InsertLog(Lang.Txt("iMass.Plat.Text"), content, "", Severity.Error);
			Shell.Invoke((MethodInvoker)delegate
			{
				MessageBox.Show(content, Lang.Txt("iMass.Plat.Text"), MessageBoxButtons.OK);
			});
		}
	}

	private void Detector_errMsg(string category, string content, string detail, string severity)
	{
		Log.InsertLog(category, content, detail, (Severity)Enum.Parse(typeof(Severity), severity));
	}

	private void CanetMsg(string category, string content, string detail, string severity)
	{
		Log.InsertLog(category, content, detail, (Severity)Enum.Parse(typeof(Severity), severity));
	}
}
